<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <title>登录</title>
    <link rel="stylesheet" href="../css/login.css">
    <script src="../lib/autoSize.js"></script>
</head>
<body>

<!--logo-->
<div class="logo"></div>

<img src="../images/login/radius.png"/><input type="text" id="phone" placeholder="请输入手机号">
<p class="message">请输入手机号</p>
<img src="../images/login/Locked.png"/><input type="password" id="pwd" placeholder="请输入密码">
<p class="message">请输入密码</p>
<button id="btn" class="btn">登录</button>

<a href="regist1.html" class="regist">还没有账号？申请注册</a>
<a href="change_pass.html" class="remove">忘记密码！</a>


<script type="text/javascript" src="../lib/jquery-3.2.1.min.js"></script>
<script src="../lib/layer.js"></script>
<script src="../js/common.js"></script>
<script type="text/javascript">
    var phone;//手机号
    var pwd;//密码
    var reg = /^1[34578]\d{9}$/;//正则，验证电话号码

//    if (window.localStorage.getItem('ruralCooperativePhone')) {
//        phone = window.localStorage.getItem('ruralCooperativePhone');
//        pwd = window.localStorage.getItem('ruralCooperativePwd');
//        $('#phone').val(phone);
//        $('#pwd').val(pwd);
//    }

//    $('#phone').blur(function () {
//        phone = this.value;
//        if (!reg.test(phone)) {
//            layer.open({
//                content: '请输入手机号！',
//                skin: 'msg',
//                time: 2
//            });
//        }
//    });
//
//    $('#pwd').blur(function () {
//        pwd = this.value;
//        if (!pwd) {
//            layer.open({
//                content: '请输入密码！',
//                skin: 'msg',
//                time: 2
//            });
//        }
//    });

    $("#btn").on("click", login);


    //      用户登录
    function login() {
        phone = $('#phone').val();
        pwd = $('#pwd').val();
        if (phone == '') {
            layer.open({
                content: '请输入手机号！',
                skin: 'msg',
                time: 2
            });
        } else if (!reg.test(phone)) {
            layer.open({
                content: '手机号格式不正确！',
                skin: 'msg',
                time: 2
            });
        }else if (!pwd) {
            layer.open({
                content: '请输入密码！',
                skin: 'msg',
                time: 2
            });
        }else{
            API.phoneLogin(phone, pwd, 1, function (data) {
                if (data.status === 1) {
                    loginToken = data.datas[0].loginToken;
                    tools.setStore(LOGIN_TOKEN_NAME, loginToken);
                    tools.setStore(LOGIN_DATA_NAME,  data.datas[0]);

                    layer.open({
                        content: data.msg,
                        skin: 'msg',
                        time: 1
                    });
                    location.href='home.html';

                } else {
                    layer.open({
                        content: '用户名或密码错误',
                        skin: 'msg',
                        time: 2
                    });
                }
            });
        }
    }
</script>
</body>
</html>
